package cn.qiyu5522.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.qiyu5522.domain.entity.SystemException;
import cn.qiyu5522.domain.entity.UserRole;
import cn.qiyu5522.enums.AppHttpCodeEnum;
import cn.qiyu5522.mapper.UserRoleMapper;
import cn.qiyu5522.service.UserRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户和角色关联表 服务实现类
 * </p>
 *
 * @author Tonkey
 * @since 2024-04-22
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {

    @Override
    public List<Long> selectRoleIdsByUserId(Long id) {
        LambdaQueryWrapper<UserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRole::getUserId,id);
        List<UserRole> userRoles = list(queryWrapper);
        if(!ObjectUtil.isNotEmpty(userRoles)){
            throw new SystemException(AppHttpCodeEnum.NO_ROLES_ASSIGNED);
        }
        List<Long> roleIds = userRoles.stream().map(ur -> ur.getRoleId()).collect(Collectors.toList());
        // 根据角色Id 查询角色信息
        return roleIds;
    }
}
